/**

 Copyright 2001-2011, Motio, Inc.
 All rights reserved.

 **/
package net.emsquared.gamedev.entity;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import net.emsquared.gamedev.TestGame;
import net.emsquared.gamedev.maps.MapCoordinates;
import net.emsquared.gamedev.screens.IScreen;
import net.emsquared.gamedev.screens.MainScreen;

public abstract class AbstractMoveableEntity extends Entity
{
   private float velocity = 95f;  //default velocity
   private boolean moving = false;
   private Move_Direction move_direction;

   private MapCoordinates destination;

   private float currentX;
   private float currentY;

   private enum Move_Direction{
      UP,
      DOWN,
      LEFT,
      RIGHT
   }

   protected AbstractMoveableEntity(String aTexturePath, IScreen aScreen)
   {
      super(aTexturePath, aScreen);
   }

   protected AbstractMoveableEntity(String aTexturePath, IScreen aScreen, int x, int y)
   {
      super(aTexturePath, aScreen, x, y);
      currentX = getXPos_Pix();
      currentY = getYPos_Pix();
   }

   public void moveUp()
   {
      MapCoordinates newMapCoordinates = new MapCoordinates(getXPos_Tile(), getYPos_Tile() + 1);
      if(!isMoving() && ((MainScreen)getScreen()).canMoveTo(newMapCoordinates))
      {
         destination = newMapCoordinates;
         move_direction = Move_Direction.UP;
         setMoving(true);
      }
   }

   public void moveDown()
   {
      MapCoordinates newMapCoordinates = new MapCoordinates(getXPos_Tile(), getYPos_Tile() - 1);
      if(!isMoving() && ((MainScreen)getScreen()).canMoveTo(newMapCoordinates))
      {
         destination = newMapCoordinates;
         move_direction = Move_Direction.DOWN;
         setMoving(true);
      }
   }

   public void moveLeft()
   {
      MapCoordinates newMapCoordinates = new MapCoordinates(getXPos_Tile() - 1, getYPos_Tile());
      if(!isMoving() && ((MainScreen)getScreen()).canMoveTo(newMapCoordinates))
      {
         destination = newMapCoordinates;
         move_direction = Move_Direction.LEFT;
         setMoving(true);
      }
   }

   public void moveRight()
   {
      MapCoordinates newMapCoordinates = new MapCoordinates(getXPos_Tile() + 1, getYPos_Tile());
      if(!isMoving() && ((MainScreen)getScreen()).canMoveTo(newMapCoordinates))
      {
         destination = newMapCoordinates;
         move_direction = Move_Direction.RIGHT;
         setMoving(true);
      }
   }

   public float getVelocity()
   {
      return velocity;
   }

   public void setVelocity(float aVelocity)
   {
      velocity = aVelocity;
   }

   public boolean isMoving()
   {
      return moving;
   }

   public void setMoving(boolean aMoving)
   {
      moving = aMoving;
   }

   public void move(float aDelta)
   {
      if(moving)
      {
         switch(move_direction)
         {
            case UP:
               currentY += velocity * aDelta;
               if(currentY > destination.getY_Pix())
               {
                  currentY = destination.getY_Pix();
                  setYPos_Tile(destination.getY());
                  setMoving(false);
               }
               break;
            case DOWN:
               currentY -= velocity * aDelta;
               if(currentY < destination.getY_Pix())
               {
                  currentY = destination.getY_Pix();
                  setYPos_Tile(destination.getY());
                  setMoving(false);
               }
               break;
            case RIGHT:
               currentX += velocity * aDelta;
               if(currentX > destination.getX_Pix())
               {
                  currentX = destination.getX_Pix();
                  setXPos_Tile(destination.getX());
                  setMoving(false);
               }
               break;
            case LEFT:
               currentX -= velocity * aDelta;
               if(currentX < destination.getX_Pix())
               {
                  currentX = destination.getX_Pix();
                  setXPos_Tile(destination.getX());
                  setMoving(false);
               }
               break;
         }
      }
   }

   @Override
   public void draw(SpriteBatch spriteBatch)
   {
      spriteBatch.draw(this, currentX, currentY, getWidth(), getHeight());
   }

   @Override
   public float getXPos_Pix()
   {
      return currentX;
   }

   @Override
   public float getYPos_Pix()
   {
      return currentY;
   }
}
